<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>电子时钟</title>
    <style>
        .ele-clock{
            padding:20px;
            border:solid 1px #333;
            display:inline-block;
        }
    </style>
</head>
<body>
    <button>来个电子时钟</button>
    <button>下去吧</button>
    <hr>

    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

    <script type="text/babel">
        //创建虚拟 DOM    启动定时器
        class Clock extends React.Component{
            //1. 声明 state
            state = {
                curTime: moment().format('YYYY-MM-DD HH:mm:ss')
            }

            //完成时回调
            componentDidMount(){
                this.timer = setInterval(() => {
                    //修改 state curTime
                    this.setState({
                        curTime: moment().format('YYYY-MM-DD HH:mm:ss')
                    })
                }, 1000);
            }

            //卸载前的回调
            componentWillUnmount(){
                //清空定时器
                clearInterval(this.timer);
            }

            render(){
                console.log(222);
                return <div className="ele-clock">
                        {this.state.curTime}
                    </div>
            }
        }
        









        //获取网页中 button
        let btns = document.querySelectorAll('button');

        btns[0].onclick = function(){
            //渲染 Image 组件
            ReactDOM.render(<Clock />, document.querySelector("#root"));
        }

        btns[1].onclick = function(){
            //卸载 移除  unmount 卸载  component 组件  at 在  node 节点
            ReactDOM.unmountComponentAtNode(document.querySelector("#root"));
        }


    </script>


</body>
</html>